<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>
<%@ taglib prefix="fn"  uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ page import="kr.co.insoft.framework.config.Authorities" %>
<c:set var="CONTEXT">${pageContext.request.contextPath}</c:set>
<c:set var="cssUrl">${pageContext.request.contextPath}/resources/css</c:set>
<c:set var="jsUrl">${pageContext.request.contextPath}/resources/js</c:set>
<div class="col-sm-12 page-header custom-page-header">
	<h1><spring:message code="menu.template.script"/></h1>
	<small><spring:message code="menu.template.script.desc"/></small>
</div>
<form:form modelAttribute="scriptEntityModel" role="form" cssClass="form-horizontal">
	<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/> 
	<form:input path="scriptid" type="hidden"/>
	<form:input path="companyid" type="hidden"/>
	<input type="hidden" id="chkName" value="Y"/>
	<input type="hidden" id="orignalName" value='${scriptEntityModel.scriptName }'/>
	<!-- 스크립트 네임 -->
	<div class="form-group">
		<label class="col-sm-2 control-label"><spring:message code="template.script.name"/></label>
		<div class="col-sm-4"><form:input path="scriptName" cssClass="form-control"/></div>
		<div class="col-sm-2"> 
			<button class="btn btn-primary btnCheckName" type="button"><spring:message code="common.check.name"/></button>
		</div>
	</div>
	<!-- 스크립트 -->
	<div class="form-group">
		<label class="col-sm-2 control-label"><spring:message code="template.script"/></label>
		<div class="col-sm-10"><form:textarea path="script"  cssClass="form-control" rows="15"/></div>
	</div>
	<!-- 설명 -->
	<div class="form-group">
		<label class="col-sm-2 control-label"><spring:message code="common.description"/></label>
		<div class="col-sm-10"><form:textarea path="scriptDesc"  cssClass="form-control" rows="2"/></div>
	</div>
	<!-- 생성 및 수정일자 -->
	<div class="form-group">
		<label class="col-sm-2 control-label"><spring:message code="common.updatedDate"/></label>
		<div class="col-sm-10">
			<p class="form-control-static">${scriptEntityModel.updatedDate }</p>
		</div>
	</div>
	<!-- 하단 버튼 -->
	<div class="form-group">  
		<div class="col-sm-12 text-right">
		<button class="btn btn-default btn_link btn_show_group" data-link="./lists" type="button"><spring:message code="button.list"/></button>
		<c:if test="${fn:contains(buttons , Authorities.UPDATE)}">
		<button class="btn btn-primary updateBtn btn_show_group" type="button"><spring:message code="button.edit"/></button>
		<button class="btn btn-success updateSubmitBtn btn_show_group" type="button"><spring:message code="button.edit"/></button>
		</c:if>
		<c:if test="${fn:contains(buttons , Authorities.REMOVE)}">
		<button class="btn btn-primary deleteBtn btn_show_group" type="button"><spring:message code="button.delete"/></button>
		</c:if>
		</div>
	</div>
</form:form>
<script type="text/javascript">  

$(document).ready(function() {

	$(".form-horizontal").find(":input").attr("disabled","true");
	$(".btn_show_group").removeAttr("disabled");
	$(".updateSubmitBtn").hide();
	/* 스크립트 수정 */
	$(document).on("click", ".updateBtn", function() {
		$(".form-horizontal").find(":input").removeAttr("disabled");
		$(".updateSubmitBtn").show();
		$(".btnCheckName").show();
		$(this).hide();
	})
	/* 스크립트 삭제 */
	.on('click', ".deleteBtn", function() {
		bootbox.confirm("<spring:message code='common.remove.confirm'/>", function(result) {
			if ( result ) {
				Loading.show();
				/* attr("disabled","true") 상태에서는 serialize() 되지 않음 */
				$(".form-horizontal").find(":input").removeAttr("disabled");
				$.post("./remove", $("#scriptEntityModel").serialize(), function(res) {
				}).fail(function() {
					bootbox.alert("<spring:message code='common.server.fail'/>");
				}).done(function(res) {
					if ( res ) {
						bootbox.alert("<spring:message code='common.remove.ok'/>", function() {
							Loading.link("./lists");							
						});
					} else {
						bootbox.alert("<spring:message code='common.remove.fail'/>");
					}
				}).always(function() {
					Loading.hide();
				});
			}
		});
	})
	/* 스크립트 수정 실행 */
	.on("click", ".updateSubmitBtn", function() {
		if($("#chkName").val() == 'N'){
			bootbox.alert("<spring:message code='common.name.unavailable'/>");
			return;
		} 
		bootbox.confirm("<spring:message code='common.update.confirm'/>", function(result) {
			if ( result ) {
				Loading.show();
				$.post("./edit", $("#scriptEntityModel").serialize(), function(res) {
				}).fail(function() {
					bootbox.alert("<spring:message code='common.server.fail'/>");
				}).done(function(res) {
					if ( res ) {
						bootbox.alert("<spring:message code='common.update.ok'/>", function() {
							Loading.link("./lists");							
						});
					} else {
						bootbox.alert("<spring:message code='common.update.fail'/>");
					}
				}).always(function() {
					Loading.hide();
				});
			}
		});
	})
	/* 중복 네임 체크 */
	.on("click", ".btnCheckName", function() {
		/* 임시 이름같은 경우 중복 체크하지 않음 */ 
		if( $("#orignalName").val()==$("#scriptName").val() ) {
			bootbox.alert("<spring:message code='common.name.available'/>");
			return;
		}
		Loading.show();
		$.post("./isExistScriptName", $("#scriptEntityModel").serialize() , function(res) {
		}).fail(function() {
			bootbox.alert("<spring:message code='common.server.fail'/>");
		}).done(function(res) {
			if (res) {
				bootbox.alert("<spring:message code='common.name.unavailable'/>");
			} else {
				bootbox.alert("<spring:message code='common.name.available'/>", function() {
					$("input[id='chkName']").val("Y");
				});
			}
		}).always(function() {
			Loading.hide();
		});
	});
	$("#scriptName").bind('input keyup mouseup keydown mousedown', function() { 
		$("input[id='chkName']").val("N");
	});
});

</script>